package querylog.util;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import querylog.bean.LogHead;

public class AppUtil {
	public static String formatXML(String inputXML) {
		StringReader in = null;		//字符串字符输入流
		StringWriter out = null;	//字符串字符输出流
		//如果报文为空，将报文内容改成“无”
		if(inputXML==null || "".equals(inputXML)){
			return "无";
		}
		//如果报文不为空，进行格式化
		try {
			SAXReader saxReader = new SAXReader();
			//创建一个串的字符输入流
			in = new StringReader(inputXML);
			Document doc = saxReader.read(in);
			//创建输出格式
			OutputFormat format = OutputFormat.createPrettyPrint();
			//创建输出
			out = new StringWriter();
			//创建输出流
			XMLWriter writer = new XMLWriter(out, format);
			//输出格式化的串到目标中，格式化后的串保存在out中
			writer.write(doc);
			//其他异常返回数据库中原文
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			//System.out.println("wxy输入报文格式有误");
			e.printStackTrace();
			return inputXML;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return inputXML;
		}finally{
			quietClose(in);
			quietClose(out);
		}
		
		
		return out.toString();
	}

	private static void quietClose(Reader reader) {
		try {
			// TODO Auto-generated method stub
			if (reader!=null) {
				reader.close();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private static void quietClose(Writer writer) {
		// TODO Auto-generated method stub
		try {
			// TODO Auto-generated method stub
			if (writer!=null) {
				writer.close();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	/**
	 * @Title textSpilt
	 * @Description 将日志明细信息切割为[]包裹的段
	 * @param @param text
	 * @param @return
	 */
	public static List<String> textSpilt(String text){
		if(text==null || "".equals(text)){
			text = "无";
		}
		List<String> list = new ArrayList<String>();
		int start =0;			//开始截取的位置
		int startFlag=0;		//左括号标志
		int endFlag=0;			//右括号标志
		for (int i = 0; i < text.length(); i++) {
			if(text.charAt(i)=='['){
				startFlag++;
				if (startFlag == endFlag +1) {
					start=i;
				}
			}else if (text.charAt(i)==']') {
				endFlag++;
				if (endFlag==startFlag) {
					list.add(text.substring(start, i+1));
				}
			}
		}
		
		return list;		
	}
	
	/**不用了
	 * @Title praseViewableXml
	 * @Description xml报文直接输出到页面<>会被转义,处理成可以在页面展示的字符串.可用xmp标签代替。启用改用apputil格式化
	 * @param @param loghead
	 * @param @return
	 */
	public LogHead praseViewableXml(LogHead loghead){
		loghead.setReqinfo(String.valueOf(loghead.getReqinfo()).replaceAll("<", "&lt;").replaceAll(">", "&gt;"));
		loghead.setRespinfo(String.valueOf(loghead.getRespinfo()).replaceAll("<", "&lt;").replaceAll(">", "&gt;"));
		System.out.println(loghead);
		return loghead;
	}
	
	public static String setAnchor(String sdf){
		
		return "";
	}
	
	public static String change(String to){
//		Pattern p = Pattern.compile("[ERROR]");
//		Matcher m = p.matcher(to);
//		
//		String result= m.replaceFirst("<font id='destination'>");
		String result =to.replaceFirst("\\[ERROR\\]", "<font id='destination'>[ERROR]</font>");
		
		return result;
	}
	
	public static void main(String[] args) {
		//String tt = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Reply_Msg><Sys_Head><TRAN_TIMESTAMP>120107592</TRAN_TIMESTAMP><USER_ID>170460</USER_ID><SOURCE_BRANCH_NO>0001</SOURCE_BRANCH_NO><BRANCH_ID>411722</BRANCH_ID><CUSTOMER_ID></CUSTOMER_ID><RET_STATUS>F</RET_STATUS><DEST_BRANCH_NO>411722</DEST_BRANCH_NO><RET_ARRAY><Row><RET_MSG>-Invalid Client_No for check holiday!</RET_MSG><RET_CODE>227076</RET_CODE></Row></RET_ARRAY><TRAN_DATE>20240423</TRAN_DATE><TRAN_MODE>ONLINE</TRAN_MODE><SEQ_NO>TLE24042317046053500</SEQ_NO><SOURCE_TYPE>MT</SOURCE_TYPE><WS_ID>001</WS_ID><TRAN_CODE>059100</TRAN_CODE><SERVER_ID>127.0.0.1</SERVER_ID><CONSUMER_ID>TLE</CONSUMER_ID><SERVICE_CODE>SVR_INQUIRY</SERVICE_CODE></Sys_Head><Body><APP_HEAD><MODULE_ID>RB</MODULE_ID><OPER_CODE>23107</OPER_CODE><APPR_FLAG></APPR_FLAG><CURRENT_NUM>0</CURRENT_NUM><PROGRAM_ID>CD0210</PROGRAM_ID><AUTH_FLAG>N</AUTH_FLAG><PAGE_START>0</PAGE_START><TRAN_TYPE></TRAN_TYPE><USER_LANG>CHINESE</USER_LANG><TOTAL_NUM>-1</TOTAL_NUM><APPR_USER_ID></APPR_USER_ID><AUTH_USER_ID></AUTH_USER_ID><PAGE_END>0</PAGE_END><PGUP_OR_PGDN>0</PGUP_OR_PGDN><REVERSAL_TRAN_TYPE></REVERSAL_TRAN_TYPE></APP_HEAD></Body></Reply_Msg>]]";
		String test = "[DEBUG][Ensemble01][com.dcsa.symbols.cbsd.aq.AQAdtLogMessageListener.onMessage(AQAdtLogMessageListener.java:139)][2017-12-13 09:13:59 044][Thread-57 ][SYS_HEAD.SEQ_NO=PAY17121301413154300 SYS_HEAD.SOURCE_TYPE=FP][SESSION_ID=2871 PROC_NAME=PT_PAYMENT_INTERFACE.qry_charge_status EVENT_INFO=rollback to [DP1400_1138_091234631058000]]";
		for (String s : textSpilt(test)) {
			System.out.println(s);
		}
		//System.out.println(textSpilt(test));
		
		//System.out.println(change("[INFO ][Ensemble01[ERROR][Ensemble]"));.
		
	}
}
